#include "choosepanel.h"
#include <QLabel>
#include <QRegExp>
#include <QFileDialog>
#include <QDebug>
#include <QMessageBox>
ChoosePanel::ChoosePanel(int * input,int * num,int * storage,bool * ok,QWidget *parent): QDialog(parent)
{
    /*接收传入的指针*/
    this->num=num;
    this->input=input;
    this->storage=storage;
    this->ok=ok;
    /*分配相应空间*/
    numberInput=new QLineEdit;
    storageMethod=new QComboBox;
    storageMethod->addItem(tr("邻接矩阵存储"));
    storageMethod->addItem(tr("邻接链表存储"));
    inputMethod=new QComboBox;
    inputMethod->addItem(tr("手动选点"));
    inputMethod->addItem(tr("随机生成"));
    inputMethod->addItem(tr("文件选择"));
    enter=new QPushButton(tr("确定"));
    cancel=new QPushButton(tr("取消"));
    layout=new QGridLayout(this);
    /*设置相应的布局*/
    layout->addWidget(new QLabel(tr("总点数")),0,0);
    layout->addWidget(numberInput,0,1);
    layout->addWidget(new QLabel(tr("存储方式")),1,0);
    layout->addWidget(inputMethod,1,1);
    layout->addWidget(new QLabel(tr("输入方式")),2,0);
    layout->addWidget(storageMethod,2,1);
    QGridLayout * butLayout=new QGridLayout();
    butLayout->addWidget(enter,0,0);
    butLayout->addWidget(cancel,0,1);
    layout->addLayout(butLayout,3,0,2,0);
    /*设置输入的正则限制*/
    QRegExp regExp("[1-4][0-9]|[2-9]");
    numberInput->setValidator(new QRegExpValidator(regExp,this));
    /*设置按钮响应*/
    connect(cancel,SIGNAL(clicked(bool)),this,SLOT(quit()));
    connect(enter,SIGNAL(clicked(bool)),this,SLOT(goon()));
    connect(inputMethod,SIGNAL(currentIndexChanged(QString)),this,SLOT(canEditNum()));
    //connect(fileButton,SIGNAL(clicked(bool)),this,SLOT(chooseFile()));
}

void ChoosePanel::quit()
{
    *ok=false;
    close();
}

void ChoosePanel::goon()
{
   // qDebug()<<storageM*input=2;ethod->currentText();
    *num=numberInput->text().toInt();
    if(inputMethod->currentText()!=tr("文件选择")&&(*num<2||*num>50))
    {
        QMessageBox::critical(this,"错误","数值应在2~50之间");     //给出错误提示
        return;
    }
    if(storageMethod->currentText()==tr("邻接矩阵存储"))
        *storage=0;
    else
        *storage=1;
    if(inputMethod->currentText()==tr("手动选点"))
        *input=0;
    else if(inputMethod->currentText()==tr("随机生成"))
        *input=1;
    else
    {
        *input=2;
        *num=100;
    }
    *ok=true;
    close();
}

void ChoosePanel::canEditNum()
{
    if(inputMethod->currentText()=="文件选择")
        numberInput->setEnabled(false);
    else
        numberInput->setEnabled(true);
}

/*
void ChoosePanel::chooseFile()
{
    QString s=QFileDialog::getOpenFileName(this,"选择背景图片","/","JPG文件(*.jpg);;PNG文件(*.png)");
    qDebug()<<s;
}
*/
